﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HCSAnalyzer.Classes.Base_Classes.DataStructures;
using System.IO;
using System.Drawing;
using System.Windows.Forms.DataVisualization.Charting;

namespace HCSAnalyzer.Classes.Base_Classes.Viewers
{
    class cTableToHTML : cComponent
    {
        public string FolderName = "";
        public bool IsDisplayUIForFilePath = false;
        public bool IsDisplayResult = false;

        cExtendedTable Input = null;

        public cTableToHTML()
        {
            base.Title = "Table to HTML";
            base.Info = "Write a table in a HTML file (if Tags are images, they are saved as well).";
        }

        public void SetInputData(cExtendedTable InputTable)
        {
            this.Input = InputTable;
        }

        public cFeedBackMessage Run()
        {


            if (this.Input == null)
            {
                base.FeedBackMessage.IsSucceed = false;
                base.FeedBackMessage.Message += ": No input defined!";
                return base.FeedBackMessage;
            }
            else
            {
                if (IsDisplayUIForFilePath)
                {
                    FolderBrowserDialog WorkingFolderDialog = new FolderBrowserDialog();
                    WorkingFolderDialog.ShowNewFolderButton = true;
                    WorkingFolderDialog.Description = "Select destination folder";
                    if (WorkingFolderDialog.ShowDialog() != DialogResult.OK)
                    {
                        FeedBackMessage.IsSucceed = false;
                        FeedBackMessage.Message = "Incorrect Folder Name.";
                        return FeedBackMessage;
                    }
                    FolderName = WorkingFolderDialog.SelectedPath;
                }

                string HTMLFileName = FolderName + "\\Index.html";
                Directory.CreateDirectory(FolderName + @"\\Images\\");

                using (StreamWriter myFile = new StreamWriter(HTMLFileName, false, Encoding.Default))
                {
                    // Export titles:  
                    string ToBeWritten = "<!DOCTYPE html><html><body><h1>DRC report : " + this.Input.Name + @"</h1><table border=1>";

                    string RowString = "<tr>";

                    if (this.Input.ListTags != null)
                    {
                        RowString += "<td>";
                        RowString += "Associated Tag";
                        RowString += "</td>";
                    }

                    if (this.Input.ListRowNames != null)
                    {
                        RowString += "<td>";
                        RowString += this.Input.Name;
                        RowString += "</td>";
                    }

                    for (int j = 0; j < this.Input.Count; j++)
                    {
                        RowString += "<td>";
                        RowString += this.Input[j].Name;
                        RowString += "</td>";
                    }

                    RowString += "</tr>";
                    ToBeWritten += RowString;


                    for (int i = 0; i < this.Input[0].Count; i++)
                    {
                        RowString = "<tr>";
                        if (this.Input.ListTags != null)
                        {
                            RowString += "<td>";

                            if (this.Input.ListTags[i].GetType() == typeof(Bitmap))
                            {
                                RowString += "Associated Tag";
                            }

                            Chart A = null;
                            try
                            {
                                A = (Chart)this.Input.ListTags[i];
                            }
                            catch (Exception)
                            {
                                throw;
                            }

                            if (A != null)
                            {
                                int TmpWidth = A.Width;
                                int TmpHeight = A.Height;

                                A.Width = 500;
                                A.Height = 300;

                                string ImageName = FolderName + @"\Images\Image" + i.ToString() + ".jpg";
                                ((Chart)this.Input.ListTags[i]).SaveImage(ImageName, ChartImageFormat.Jpeg);

                                A.Width = TmpWidth;
                                A.Height = TmpHeight;

                                RowString += "<img src=\"Images\\Image" + i.ToString() + ".jpg\">";
                            }
                            else
                            {
                                RowString += "n.a.";
                            }

                            RowString += "</td>";
                        }

                        if (this.Input.ListRowNames != null)
                        {
                            RowString += "<td>";
                            RowString += this.Input.ListRowNames[i];
                            RowString += "</td>";
                        }


                        for (int j = 0; j < this.Input.Count; j++)
                        {
                            RowString += "<td>";
                            RowString += this.Input[j][i].ToString();
                            RowString += "</td>";
                        }

                        RowString += "</tr>";
                        ToBeWritten += RowString;
                    }

                    ToBeWritten += "</table>";

                    ToBeWritten += "</body></html>";
                    myFile.Write(ToBeWritten);
                    myFile.Close();
                }

                if (IsDisplayResult)
                {
                    System.Diagnostics.Process proc = new System.Diagnostics.Process();
                    if (GlobalInfo.OptionsWindow.radioButtonIE.Checked)
                        proc.StartInfo.FileName = "iexplore";
                    else
                        proc.StartInfo.FileName = "chrome";
                    proc.StartInfo.Arguments = HTMLFileName;
                    proc.Start();
                }

            }
            return base.FeedBackMessage;
        }

    }
}
